package edu.ncsu.csc.ip.client.run;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import edu.ncsu.csc.ip.client.PeerClient;
import edu.ncsu.csc.ip.common.Log;

public class RunTask2 {

	public static void main(String[] args) {

		String[] addressStrings = {"152.1.13.82", "152.46.16.215", "152.1.13.83"};

		long startTime = 0;
		long endTime = 0;
		long duration = 0;
		long avg = 0;
		String filename = "test2.txt";
		int MSS = 200;
		int port = 65425;
		
		InetAddress[] addressList = new InetAddress[addressStrings.length];
		
		for(int i = 0; i < (addressStrings.length); i++){
			try {
				addressList[i] = InetAddress.getByName(addressStrings[i]);
			} catch (UnknownHostException e) {
				Log.println("IP Address: " + addressStrings[i] + " is malformed.");
				System.exit(0);
			}
		}
		while (MSS >= 100) {
			PeerClient pc = new PeerClient(addressList, port, filename, MSS);
			Log.println("Starting test");
			try {
				
				
				for (int i = 0; i < 4; i++) {
					startTime = System.currentTimeMillis();
					pc.runClient();
					endTime = System.currentTimeMillis();
					duration = endTime - startTime;
					avg += duration;
					File file = new File("./Task2_" + MSS
							+ "_MSS_round"+i+"_results.csv");
					PrintWriter printWrite = new PrintWriter(new FileWriter(file,
							false));
					printWrite.println("Test# , Time in ms");
					printWrite.println(i + "," + duration);
					printWrite.close();
				}
//				avg = avg / 5;
//				printWrite.println("Average" + "," + avg);
//				printWrite.println("Average in seconds" + "," + avg / 1000);
//				printWrite.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Log.println("End of test");
			MSS -= 100;
			avg = 0;
		}
		System.exit(0);
	}
}
